package net.ch2.pc12.tech.ackborn.activity;

import android.R.id;
import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.os.Bundle;
import android.os.Handler;

import net.ch2.pc12.tech.ackborn.R;
import net.ch2.pc12.tech.ackborn.activity.helper.MenuHelper;
import net.ch2.pc12.tech.ackborn.activity.thread.BoardListThread;
import net.ch2.pc12.tech.ackborn.dto.ProgressDTO;
import net.ch2.pc12.tech.ackborn.model.BbsModel;

public class TopActivity extends ExpandableListActivity implements Runnable,OnClickListener{
    /** Called when the activity is first created. */

	private SimpleExpandableListAdapter mAdapter;
	
	private ProgressDTO progressDTO;

	public ProgressDTO getProgressDTO() {
		return progressDTO;
	}

	public void setProgressDTO(ProgressDTO progressDTO) {
		this.progressDTO = progressDTO;
	}

	TextView statusView;

	BoardListThread thread;
	private BbsModel model;
	public BbsModel getModel() {
		return model;
	}

	public void setModel(BbsModel model) {
		this.model = model;
	}

	private final Handler handler = new Handler();
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ExpandableListView list = (ExpandableListView)findViewById(id.list);
        list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {

				TextView tv = (TextView)v.findViewById(R.id.BoardRowBoardId);
				String bid = tv.getText().toString();
				tv = (TextView)v.findViewById(R.id.BoardRowBoardName);				
				String bname = tv.getText().toString();
				tv = (TextView)v.findViewById(R.id.BoardRowBoardUrl);
				String burl = tv.getText().toString();
				
				Intent intent  = new Intent(getApplicationContext(),BoardActivity.class);
				intent.putExtra("boardId", bid);
				intent.putExtra("boardName",bname);
				intent.putExtra("boardUrl", burl);
				startActivity(intent);

				return false;
			}

        });
        statusView = (TextView)findViewById(R.id.TopStatus);
        statusView.setText(R.string.BoardList);
        
        if(BbsModel.hasCache(this)){
        	setList(false);
        }else{

        	// 確認ダイアログだして、取得するかを聞く
        	Builder builder = new Builder(this);
        	builder.setTitle(R.string.NetworkConfirm);
        	builder.setMessage(R.string.BoardConfirm);
        	builder.setPositiveButton(R.string.ConfirmOK, this);
        	
        	builder.setNegativeButton(R.string.ConfirmNG,this);
        	
        	builder.setCancelable(false);
        	final AlertDialog dialog = builder.create();
        	dialog.show();
        }
        
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		boolean result = super.onCreateOptionsMenu(menu);
		MenuHelper.setMenu(menu, MenuHelper.TYPE_BOARD_LIST);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		
		case MenuHelper.MENU_TO_TOP:
			toTop();
			return true;
		case MenuHelper.MENU_TO_BOTTOM:
			toBottom();
			return true;
		case MenuHelper.MENU_RELOAD:
			reload();
			return true;
		case MenuHelper.MENU_BOOKMARK:
			bookmark();
			return true;
		case MenuHelper.MENU_CONFIG:
			config();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void toTop(){
		ExpandableListView list= (ExpandableListView)findViewById(id.list);
		list.setSelectedGroup(0);
	}
	
	private void toBottom(){
		ExpandableListView list= (ExpandableListView)findViewById(id.list);
		list.setSelectedGroup(model.getGroupLength());
	}
	
	private void reload(){
		setList(true);
	}
	
	private void bookmark(){
		
	}
	
	private void config(){

	}
	
	private void setList(boolean reload){
		progressDTO = new ProgressDTO();
		progressDTO.setProgress(0);
		statusView.setText(R.string.LoadStart);
		thread = new BoardListThread(handler,this,reload);
		thread.start();
	}

	private void displayList(){
		mAdapter = new SimpleExpandableListAdapter(
				this,
				model.getGroupNameList(),	// groupData describes the first-level entries
				R.layout.boardlistgroup,	// Layout for the first-level entries
				new String[] { "groupName" },	// Key in the groupData maps to display
				new int[] { R.id.BoardGroupName },		// Data under "colorName" key goes into this TextView
				model.getGroupedChildNameList(),	// childData describes second-level entries
				R.layout.boardlistrow,	// Layout for second-level entries
				new String[] { "boardName", "boardId","boardUrl" },	// Keys in childData maps to display
				new int[] { R.id.BoardRowBoardName, R.id.BoardRowBoardId , R.id.BoardRowBoardUrl }	// Data under the keys above go into these TextViews
			);
        setListAdapter(mAdapter);	
        toTop();
	}
	
	public void run(){
		statusView.setText(progressDTO.getProgressMessage());
		if(progressDTO.getProgress() == ProgressDTO.COMPLETE){
			statusView.setText(R.string.BoardList);
			displayList();
		}
	}

	public void onClick(DialogInterface dialog, int which) {
		if(which == DialogInterface.BUTTON_POSITIVE){
			setList(true);
		}
		dialog.dismiss();
	}
}